//抓取北京市住房和城乡建设委员会

package main

import (
	"fmt"
	goq "lib/goquery"
	"lib/request"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"
)

var (
	main_domain = "http://www.bjjs.gov.cn" //主域名
	LOG         = "www.bjjs.gov.cn"
	NumCPU      = runtime.NumCPU()
	waitgroup   = &sync.WaitGroup{}
)

func init() {
	runtime.GOMAXPROCS(NumCPU)
}

func main() {
	url := fmt.Sprintf("%s/tabid/2184/Default.aspx", main_domain)
	for curpage := 1; curpage <= 287; curpage++ {
		querystr := fmt.Sprintf("ess$ctr5139$FDCJY_SSHouse_Model$ctl00$pageIndex=%d", curpage)
		querystr += fmt.Sprintf("&ess$ctr5139$FDCJY_SSHouse_Model$ctl00$ImageButton1.x=11")
		querystr += fmt.Sprintf("&ess$ctr5139$FDCJY_SSHouse_Model$ctl00$ImageButton1.y=7")
		querystr += fmt.Sprintf("&__VIEWSTATE=/wEPDwUKLTQwNjIwODE0Mg9kFgRmDxYCHgRUZXh0BT48IURPQ1RZUEUgSFRNTCBQVUJMSUMgIi0vL1czQy8vRFREIEhUTUwgNC4wIFRyYW5zaXRpb25hbC8vRU4iPmQCAg9kFgICAQ9kFgICBA9kFgJmD2QWDmYPZBYCAgEPZBYCAgEPDxYCHgdWaXNpYmxlaGRkAgEPZBYCAgIPZBYCAgEPDxYCHwFoZGQCAw9kFgICAQ9kFgICAQ8PFgIfAWhkZAIED2QWBAIBD2QWAgIBDw8WAh8BaGRkAgMPZBYCAgEPDxYCHwFoZGQCBg9kFgICAQ9kFgJmDw8WAh8BaGRkAgcPZBYCAgIPZBYEAgEPDxYCHwFoZGQCAw9kFgICAg9kFgJmD2QWAgIBD2QWCgIJDw8WBB8ABQfpppYg6aG1HgdFbmFibGVkZ2RkAgoPDxYEHwAFCeS4iuS4gOmhtR8CZ2RkAg0PDxYCHwAFATNkZAIODw8WAh8ABQMyODdkZAIQDw8WAh8ABQQyODY5ZGQCCA9kFgICAQ9kFgICAQ8PFgIfAWhkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WCAU1ZXNzJGN0cjUxMzkkRkRDSllfU1NIb3VzZV9Nb2RlbCRjdGwwMCRDaGVja0JveExpc3QxJDAFNWVzcyRjdHI1MTM5JEZEQ0pZX1NTSG91c2VfTW9kZWwkY3RsMDAkQ2hlY2tCb3hMaXN0MSQxBTVlc3MkY3RyNTEzOSRGRENKWV9TU0hvdXNlX01vZGVsJGN0bDAwJENoZWNrQm94TGlzdDEkMgU1ZXNzJGN0cjUxMzkkRkRDSllfU1NIb3VzZV9Nb2RlbCRjdGwwMCRDaGVja0JveExpc3QxJDMFNWVzcyRjdHI1MTM5JEZEQ0pZX1NTSG91c2VfTW9kZWwkY3RsMDAkQ2hlY2tCb3hMaXN0MSQzBTJlc3MkY3RyNTEzOSRGRENKWV9TU0hvdXNlX01vZGVsJGN0bDAwJEltYWdlQnV0dG9uMgUyZXNzJGN0cjUxMzkkRkRDSllfU1NIb3VzZV9Nb2RlbCRjdGwwMCRJbWFnZUJ1dHRvbjMFMmVzcyRjdHI1MTM5JEZEQ0pZX1NTSG91c2VfTW9kZWwkY3RsMDAkSW1hZ2VCdXR0b24xsceieOJ6ybJUCceqXVngaEvAU0k=")
		doc, e := postDoc(url, querystr)
		if e != nil {
			errlog("{"+querystr+"} 失败:" +  e.Error())
			continue
		}
		doc.Find("#ess_ctr5139_FDCJY_SSHouse_Model_ctl00_addControl tr").Each(func(i int, sec *goq.Selection) {
			if i == 0 {
				return
			}
			link, ok := sec.Find("td").Eq(0).Find("a").Attr("href")
			if ok {
				getCompanyInfo(link)
			}
		})
		time.Sleep(time.Second * 3)
	}

	s(time.Now().Format("2006-01-02 15:04:05"), "done.")
}

//读取公司信息
func getCompanyInfo(url string) {
	tmpurl := fmt.Sprintf("%s%s", main_domain, url)
	doc, e := getDoc(tmpurl)
	if e != nil {
		errlog("{" + tmpurl + "} 失败:" + e.Error())
		return
	}
	tr := doc.Find("#ess_ctr5143_FDCJY_SSHouse_Model_ctl00_addCountorl tr")
	if tr.Length() != 14 {
		errlog("{" + tmpurl + "} 14行失败:" + e.Error())
		return
	}
	var (
		companyname  string //单位名称
		addr         string //注册地址
		ziben        string //注册资本
		frdb         string //法定代表人姓名
		last_hz_time string //最新换证时间
		tel          string //业务联系电话
	)

	companyname = filterStr(tr.Eq(0).Children().Eq(1).Text())
	addr = filterStr(tr.Eq(2).Children().Eq(1).Text())
	ziben = filterStr(tr.Eq(3).Children().Eq(1).Text())
	frdb = filterStr(tr.Eq(4).Children().Eq(1).Text())
	last_hz_time = filterStr(tr.Eq(6).Children().Eq(1).Text())
	tel = filterStr(tr.Eq(11).Children().Eq(1).Text())

	str := companyname
	str += "#" + addr
	str += "#" + ziben
	str += "#" + frdb
	str += "#" + last_hz_time
	str += "#" + tel
	outlog(str)
}

//getdoc
func getDoc(url string) (*goq.Document, error) {
	res, e := request.Get(url)
	if e != nil {
		return nil, e
	}
	doc, e := goq.NewDocumentFromResponse(res)
	if e != nil {
		return nil, e
	}
	return doc, nil
}

//postdoc
func postDoc(url string, querystr string) (*goq.Document, error) {
	res, e := request.Post(url, querystr)
	if e != nil {
		return nil, e
	}
	doc, e := goq.NewDocumentFromResponse(res)
	if e != nil {
		return nil, e
	}
	return doc, nil
}

func filterStr(str string) string {
	str = strings.Replace(str, "\r\n", "", -1)
	str = strings.Replace(str, "\r", "", -1)
	str = strings.Replace(str, "\n", "", -1)
	str = strings.Replace(str, "#", "", -1)
	str = strings.TrimSpace(str)
	return str
}

func s(a ...interface{}) {
	fmt.Println(a)
}

//outlog
func outlog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_log.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}

//errlog
func errlog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_err.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}

//debuglog
func debuglog(str string) {
	pwd, _ := os.Getwd()
	outlog := fmt.Sprintf("%s/%s_debug.txt", pwd, LOG)
	file, err := os.OpenFile(outlog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
	defer file.Close()
	if err != nil {
		return
	}
	str += "\n"
	file.WriteString(str)
}
